import axios from 'axios'
import Vue from 'vue';
import router from '../router'
import {Message} from 'element-ui'
axios.interceptors.response.use(
    response => {
      // res是响应的结果
      switch (response.data.code) {
        case 401:
          // 登录失效
          // 响应成功的拦截
          console.log('响应拦截器：')
          // console.log(response.data)
          Vue.prototype.$message.error(response.data.msg)
          break
        case 404:
          if (response.data.message !== null) {
            Vue.prototype.$message.error(response.data.msg)
          } else {
            Vue.prototype.$message.error('未知错误')
          }
          break
        case 500:
          // 错误
          if (response.data.msg !== null) {
            Vue.prototype.$message.error(response.data.msg)
          } else {
            Vue.prototype.$message.error('未知错误')
          }
          break
        case 10001:
            Vue.prototype.$message.error(response.data.msg)
            window.location.href = "https://oss.d-y-h.cn/mp/oauth2/login";
        default:
          return response
      }
      return response
    },
    err => {
      if (err.response.data.msg) {
        Vue.prototype.$message.error(err.response.data.msg)
        return Promise.reject(err.response.data.msg) // 返回接口返回的错误信息
      } else {
        // 返回状态码不为200时候的错误处理
        Vue.prototype.$message.error(err.toString())
        return Promise.resolve(err)
      }
    }
  )